﻿package airdnd.world {
	import flash.events.*;
	
	import airdnd.world.WorldTile;
	import airdnd.controls.MapControls;
	
	import flash.display.MovieClip;
	
	import com.inruntime.utils.*;
	import airdnd.WorldDesign;

	public class WorldHandler extends MovieClip {
		private var global:Global = Global.getInstance();
		private var tileID:int = 0;
		public static var tileArray:Array = new Array();
		var mouseControls:MapControls = new MapControls();
		//MapMouse are all the mouse controls associated with editing the map
		
		public function WorldHandler(xnum:int = 15, ynum:int = 11) {
			var xposition:int = 0;
			var yposition:int = 0;
			for(var i:Number=0; i<ynum; i++){ //for every 11 rows
				
				for(var j:Number=0; j<xnum; j++){ //draw 15 columns
					var tile = new WorldTile();
					
					addChild(tile);
					tile.x=j*airdnd.WorldDesign.tileDim;
					tile.y=i*airdnd.WorldDesign.tileDim;
					tileArray[tileID] = [tile, tile.x, tile.y, xposition, yposition, 0xFFFFFF];
					tile.tileID = tileID;
					tileID++;
					tile.addEventListener(MouseEvent.MOUSE_DOWN, changeTile);
					yposition++;
				}
				yposition = 0;
				xposition++;
			}
			//trace(tileArray[29]);
			
		}//end WorldHandler
		
		public function changeTile(evt:MouseEvent){
			if(MapControls.paintMode == true){
				var selectedTile:int = evt.currentTarget.tileID;
				removeChild(tileArray[selectedTile][0]);
				
				var newTile = new WorldTile(global.tileCol);
				addChild(newTile);
				newTile.x = tileArray[selectedTile][1];
				newTile.y = tileArray[selectedTile][2];
				tileArray[selectedTile].splice(0,1,newTile);
				tileArray[selectedTile].splice(5,1,global.tileCol);
				//trace(tileArray[selectedTile]);
			}
		}
	}	
}
